package com.huxiu;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.huxiu.utils.Constants;
import com.huxiu.utils.UmenEvent;
import com.huxiu.utils.Utils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.analytics.MobclickAgent;

/**
 * Created by mfp on 15-12-22.
 */
public class BrowserActivity extends Activity implements GestureDetector.OnGestureListener{
    @ViewInject(R.id.webview)
    private WebView mWebView;

    @ViewInject(R.id.layout_back)
    private RelativeLayout layout_back;

    @ViewInject(R.id.img_browser_back)
    private ImageView img_browser_back;

    @ViewInject(R.id.img_browser_next)
    private ImageView img_browser_next;

    @ViewInject(R.id.layout_share)
    private ImageView layout_share;

    private String url;

    private GestureDetector detector;
    private int flingWidth;

    private BrowserActivity instance;

    private static final String TAG = BrowserActivity.class.getName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_browser);
        instance=this;
        ViewUtils.inject(instance);
        initweb();
        Intent intent = getIntent();
        url= intent.getStringExtra("url");
        if (!TextUtils.isEmpty(url)) {
            mWebView.loadUrl(url);
        }
    }



    private void initweb(){
        mWebView.setWebViewClient(new InnerWebViewClient());
        mWebView.getSettings().setDefaultTextEncodingName("UTF-8");
        mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        mWebView.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
    }


    private ProgressDialog progress;

    private void showProgress() {
        if(progress == null) {
            progress = new ProgressDialog(this);
        }
        progress.setMessage(getString(R.string.loading_please_wait));
        progress.show();
    }

    private void dismissProgress() {
        if(progress!=null) {
            progress.dismiss();
        }
    }


    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }


    @OnClick({R.id.layout_back,R.id.img_browser_back,R.id.img_browser_next,R.id.layout_share})
    public void monClick(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                finish();
                break;
            case R.id.img_browser_back:
                if (mWebView.canGoBack()) {
                    mWebView.goBack();
                }
                break;
            case R.id.img_browser_next:
                if (mWebView.canGoForward()) {
                    mWebView.goForward();
                }
                break;
            case R.id.layout_share:
                new AsyncTask<Void, Void, Void>() {
                    Bitmap bmp = null;

                    @Override
                    protected Void doInBackground(Void... params) {
                        Resources res=getResources();

                         bmp= BitmapFactory.decodeResource(res, R.drawable.icon_iconhuxiu);
                        //                        bmp = ImageLoader.getInstance().loadImageSync("https://gd2.alicdn.com/bao/uploaded/i2/TB1uH32KpXXXXcqXFXXXXXXXXXX_!!0-item_pic.jpg");
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void aVoid) {
                        super.onPostExecute(aVoid);
                        Utils.launchShareActivity(instance,
                                url, url,
                               "doc", url,
                                url, bmp, UmenEvent.ARTICLE_SHARE);
                    }
                }.execute();
                break;
        }
    }


    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        if (e1 == null || e2 == null) {
            return false;
        }
        if (e2.getX() - e1.getX() > flingWidth && Math.abs(velocityX) > Constants.FLING_MIN_VELOCITY) {
            setResult(RESULT_CANCELED);
            finish();
            return true;
        }
        return false;
    }

    private class InnerWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);

            Log.e("aaaa", "shouldOverrideUrlLoading");
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            Log.e("aaaa", "onPageStarted");

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            dismissProgress();
            Log.e("aaaa", "onPageFinished");
            uichange();
        }
    }





    public void  uichange(){
        if(mWebView.canGoBack()){
            img_browser_back.setImageResource(R.drawable.icon_browser_back);
        }else{
            img_browser_back.setImageResource(R.drawable.icon_browser_unback);
        }
        if(mWebView.canGoForward()){
            img_browser_next.setImageResource(R.drawable.icon_browser_next);
        }else{
            img_browser_next.setImageResource(R.drawable.icon_browser_unnext);
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("SplashScreen");
        MobclickAgent.onResume(this);
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("SplashScreen");
        MobclickAgent.onPause(this);
    }
}
